package com.bianguo.android.tencentpushdemo.view.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;
import android.widget.Toast;

import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.alibaba.android.vlayout.layout.DefaultLayoutHelper;
import com.alibaba.android.vlayout.layout.FixLayoutHelper;
import com.alibaba.android.vlayout.layout.GridLayoutHelper;
import com.alibaba.android.vlayout.layout.OnePlusNLayoutHelper;
import com.alibaba.android.vlayout.layout.ScrollFixLayoutHelper;
import com.bianguo.android.tencentpushdemo.R;
import com.bianguo.android.tencentpushdemo.adapters.VLayoutAdapter;
import com.bianguo.android.tencentpushdemo.entity.ItemData;
import com.bianguo.android.tencentpushdemo.utils.DBHelper;
import com.bianguo.android.tencentpushdemo.utils.MyLog;
import com.scwang.smartrefresh.header.PhoenixHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class VLayoutActivity extends BaseActivity implements VLayoutAdapter.OnLongClickItemListener, VLayoutAdapter.OnItemClickListener {

    @BindView(R.id.vlayout_recycler)
    RecyclerView mRecyclerView;

    private VLayoutAdapter adapter;
    private ArrayList<ItemData> datas;
    private ItemTouchHelper mItemTouchHelper;
    private DBHelper dbHelper;

    @Override
    public int layoutId() {
        return R.layout.activity_vlayout;
    }

    @Override
    public void initView(Bundle savedInstanceState) {

        VirtualLayoutManager layoutManager = new VirtualLayoutManager(mContext);
        mRecyclerView.setLayoutManager(layoutManager);

        List<LayoutHelper> helpers = new LinkedList<>();
        GridLayoutHelper gridLayoutHelper = new GridLayoutHelper(3);
        gridLayoutHelper.setItemCount(6);

        GridLayoutHelper gridLayoutHelper1 = new GridLayoutHelper(2);
        gridLayoutHelper1.setItemCount(4);
        helpers.add(gridLayoutHelper);
        helpers.add(gridLayoutHelper1);
        layoutManager.setLayoutHelpers(helpers);

        dbHelper = new DBHelper(mContext, DBHelper.DB_VERSION);
        datas = new ArrayList<>();
        ArrayList<ItemData> itemDatas = dbHelper.getItemDatas(dbHelper.getReadableDatabase());
        if (itemDatas != null && itemDatas.size() > 0) {
            datas.addAll(itemDatas);
        } else {
            for (int i = 0; i < 10; i++) {
                ItemData itemData = new ItemData();
                itemData.setIndex(i);
                itemData.setContent("item " + i);
                itemData.setImageId(R.drawable.me_checked);
                dbHelper.insertItem(dbHelper.getWritableDatabase(), itemData);
            }
            datas.addAll(dbHelper.getItemDatas(dbHelper.getReadableDatabase()));
        }
        adapter = new VLayoutAdapter(layoutManager, mContext, datas);
        mRecyclerView.setAdapter(adapter);


        mItemTouchHelper = new ItemTouchHelper(new ItemTouchHelper.Callback() {
            @Override
            public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
                if (recyclerView.getLayoutManager() instanceof VirtualLayoutManager) {
                    final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN |
                            ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
                    final int swipeFlags = 0;
                    return makeMovementFlags(dragFlags, swipeFlags);
                } else {
                    final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
                    final int swipeFlags = 0;
                    return makeMovementFlags(dragFlags, swipeFlags);
                }
            }

            @Override
            public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
                //得到当拖拽的viewHolder的Position
                int fromPosition = viewHolder.getAdapterPosition();
                //拿到当前拖拽到的item的viewHolder
                int toPosition = target.getAdapterPosition();
                if (fromPosition < toPosition) {
                    for (int i = fromPosition; i < toPosition; i++) {
                        Collections.swap(datas, i, i + 1);
                    }
                } else {
                    for (int i = fromPosition; i > toPosition; i--) {
                        Collections.swap(datas, i, i - 1);
                    }
                }
                int size = datas.size();
                for (int i = 0; i < size; i++) {
                    ItemData itemData = datas.get(i);
                    itemData.setIndex(i);
                    dbHelper.updateItemData(dbHelper.getWritableDatabase(), itemData);
                }
                adapter.notifyItemMoved(fromPosition, toPosition);
                return true;
            }

            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {

            }

            @Override
            public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
                if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
//                    viewHolder.itemView.setBackgroundColor(Color.LTGRAY);
                    viewHolder.itemView.setBackgroundColor(0);
                }
                super.onSelectedChanged(viewHolder, actionState);
            }

            @Override
            public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
                super.clearView(recyclerView, viewHolder);
                viewHolder.itemView.setBackgroundColor(0);
            }

            @Override
            public boolean isLongPressDragEnabled() {
                return false;
            }
        });
        mItemTouchHelper.attachToRecyclerView(mRecyclerView);
        adapter.setOnLongClickItemListener(this);
        adapter.setOnItemClickListener(this);
    }

    @Override
    public void initData() {

    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    public void onLongClickItem(VLayoutAdapter.ViewHolder vh) {
        mItemTouchHelper.startDrag(vh);
        if (vh.getLayoutPosition() != 0 && vh.getLayoutPosition() != 1) {

        }
    }

    @Override
    public void onItemClick(View view, int position) {
        dbHelper.deleteItem(dbHelper.getWritableDatabase(), datas.get(position));
        datas.remove(position);
        adapter.notifyItemRemoved(position);
    }

    @OnClick({R.id.vlayout_nearest1, R.id.vlayout_nearest2, R.id.vlayout_nearest3, R.id.vlayout_nearest4})
    public void onClickBk(View view) {
        if (datas.size() >= 10) {
            Toast.makeText(mContext, "最多只能有10项", Toast.LENGTH_SHORT).show();
            return;
        }
        switch (view.getId()) {
            case R.id.vlayout_nearest1:
                ItemData item1 = new ItemData();
                item1.setIndex(datas.size());
                item1.setContent("最近1");
                item1.setImageId(R.mipmap.ic_launcher);
                datas.add(item1);
                adapter.notifyItemInserted(datas.size() - 1);
                dbHelper.insertItem(dbHelper.getWritableDatabase(), item1);
                break;
            case R.id.vlayout_nearest2:
                ItemData item2 = new ItemData();
                item2.setIndex(datas.size());
                item2.setContent("最近2");
                item2.setImageId(R.mipmap.ic_launcher);
                datas.add(item2);
                adapter.notifyItemInserted(datas.size() - 1);
                dbHelper.insertItem(dbHelper.getWritableDatabase(), item2);
                break;
            case R.id.vlayout_nearest3:
                ItemData item3 = new ItemData();
                item3.setIndex(datas.size());
                item3.setContent("最近3");
                item3.setImageId(R.mipmap.ic_launcher);
                datas.add(item3);
                adapter.notifyItemInserted(datas.size() - 1);
                dbHelper.insertItem(dbHelper.getWritableDatabase(), item3);
                break;
            case R.id.vlayout_nearest4:
                ItemData item4 = new ItemData();
                item4.setIndex(datas.size());
                item4.setContent("最近4");
                item4.setImageId(R.mipmap.ic_launcher);
                datas.add(item4);
                adapter.notifyItemInserted(datas.size() - 1);
                dbHelper.insertItem(dbHelper.getWritableDatabase(), item4);
                break;
        }
        datas.clear();
        datas.addAll(dbHelper.getItemDatas(dbHelper.getWritableDatabase()));
    }
}
